//
//  WYCLoginController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCLoginController.h"
#import "WYCRegisterController.h"
#import "WYCForgetController.h"
#import "XMGTabBarController.h"
#import "WYCRealnameController.h"
#import "RSAUtil.h"

@interface WYCLoginController ()


/** 账号*/
@property(nonatomic,strong)UITextField * userField;

/** 密码*/
@property(nonatomic,strong)UITextField * passwordField;
@end

@implementation WYCLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //创建界面
    [self setupCreat];
}


#pragma make ------->>创建界面
- (void)setupCreat{
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //logo
    UIImageView * logoImageView = [[UIImageView alloc] init];
    logoImageView.frame = CGRectMake(ScreenWidth / 2-55 , ScreenWidth / 2-45, 110,91 );
    logoImageView.image = [UIImage imageNamed:@"96"];
    [self.view addSubview:logoImageView];
    
    
    
    
    UIView * phoneView = [[UIView alloc] init];
    phoneView.frame = CGRectMake(40 , CGRectGetMaxY(logoImageView.frame) + 200 * px, ScreenWidth-80, 40);
    phoneView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:phoneView];
    //手机号
    UIImageView * iphonelabel = [[UIImageView alloc] init];
    iphonelabel.frame = CGRectMake(10, 8, 25 , 25);
    iphonelabel.image = [UIImage imageNamed:@"icon_phone"];
    [phoneView addSubview:iphonelabel];
    
    UITextField * userTextField = [[UITextField alloc] init];
    userTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 20 * px,10, phoneView.width-60, 20);
    userTextField.backgroundColor = [UIColor clearColor];
    userTextField.placeholder = @"手机号";
    userTextField.tintColor = wycMain;
    userTextField.font = [UIFont systemFontOfSize:15];
    userTextField.keyboardType = UIKeyboardTypeNumberPad;
    userTextField.clearButtonMode=UITextFieldViewModeAlways;
    [phoneView addSubview:userTextField];
    self.userField = userTextField;
    UIView  *lin =[[UIView alloc]init];
    lin.frame = CGRectMake(40, 39,phoneView.width-40, 1);
    lin.backgroundColor = WYCbgColor;
    [phoneView addSubview:lin];
    
    
    UIView * userView = [[UIView alloc] init];
    userView.frame = CGRectMake(40 , phoneView.y+60, ScreenWidth-80,40);
    userView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:userView];
    
    
    
    //密码
    UIImageView * passlabel = [[UIImageView alloc] init];
    passlabel.frame = CGRectMake(10, 8, 25, 25);
    passlabel.image = [UIImage imageNamed:@"icon_password"];
    [userView addSubview:passlabel];
    
    
    UITextField * passTextField = [[UITextField alloc] init];
    passTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 20 * px,10, (userView.width - iphonelabel.width + 10 * px) / 2, 20);
    passTextField.backgroundColor = [UIColor clearColor];
    passTextField.placeholder = @"密码";
    passTextField.font = [UIFont systemFontOfSize:15];
    passTextField.tintColor = wycMain;
    passTextField.secureTextEntry = YES;
    [userView addSubview:passTextField];
    self.passwordField = passTextField;

    UIView  *lin1 =[[UIView alloc]init];
    lin1.frame = CGRectMake(40,39 , userView.width-40, 1);
    lin1.backgroundColor = WYCbgColor;
    [userView addSubview:lin1];
    

    
    //登录按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(userView.x, CGRectGetMaxY(userView.frame) + 60, userView.width, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 3;
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    
    //注册按钮
    UIButton * regsterButton = [[UIButton alloc] init];
    regsterButton.frame = CGRectMake(loginButton.x, CGRectGetMaxY(loginButton.frame) + 40 * px, 100, 50 * px);
    regsterButton.layer.cornerRadius = 3;
    [regsterButton.layer setMasksToBounds:YES];
    regsterButton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];

    [regsterButton setTitle:@"立即注册 " forState:UIControlStateNormal];
    [regsterButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [regsterButton addTarget:self action:@selector(rigesterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:regsterButton];
    //忘记密码？
    UIButton * forgetButton = [[UIButton alloc] init];
    forgetButton.frame = CGRectMake(ScreenWidth- 130, CGRectGetMaxY(loginButton.frame) + 40 * px, 100, 50 * px);
    forgetButton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(forgetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [forgetButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.view addSubview:forgetButton];
   
    
}

#pragma make -------->>快速注册
- (void)rigesterClick{
    WYCRegisterController * rigester = [[WYCRegisterController alloc] init];
    [self presentViewController:rigester animated:NO completion:nil];
    WYCLogFunc;
}

#pragma make --------->>忘记密码
- (void)forgetButtonClick{
    WYCLogFunc;
    WYCForgetController * forget = [[WYCForgetController alloc] init];
    [self presentViewController:forget animated:NO completion:nil];
}
- (void)loginanimotion
{
    [self loginButtonClick];
    
}
- (void)loginButtonClick{
    
    if (self.userField.text.length == 0){
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        return;
    }
    if (self.passwordField.text.length == 0){
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
        return;
    }
    
    
    //RSA密码加密
    NSString * passStr = [NSString stringWithFormat:@"%@",self.passwordField.text];
    NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.userField.text;
    
    
    //使用RSA加密操作的暗文
    params[@"password"] = pas;
    [HttpTool POST:Login params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        WYCLog(@"------------------%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0){
            NSDictionary * dict = [responseObject objectForKey:@"result"];
            
            if(![[dict allKeys] containsObject:@"token"])//_roaldSearchText
            {
                WYCRealnameController * authen = [[WYCRealnameController alloc] init];
                if ([dict[@"regState"] intValue] == 7) {
                    authen.regState = @"1";
                }else{
                    authen.regState = dict[@"regState"];
                }
                [self presentViewController:authen animated:NO completion:nil];
                
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
                
                [WYCAccountTool saveAccount:Account];
                
                [WYCIsAppLoginTool saveIsAppLogin:YES];
                
            }else{
                XMGTabBarController * TaBbar = [[XMGTabBarController alloc] init];
                [self presentViewController:TaBbar animated:YES completion:nil];
                
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
                
                [WYCAccountTool saveAccount:Account];
                
                [WYCIsAppLoginTool saveIsAppLogin:YES];
            }
            
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
    
}



//点击空白推荐盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_userField resignFirstResponder];
    [_passwordField resignFirstResponder];
}

@end
